package com.jic.member.impl.power;
import	java.util.List;
import	java.util.ArrayList;

import java.lang.Long;
import java.util.Date;


import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.request.ChildNameRequest;
import com.jic.manager.request.SysDeptRequest;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.entity.power.MegradeRule;
import com.jic.member.entity.power.MememberLegalRule;
import com.jic.member.mapper.power.MegradeRuleMapper;
import com.jic.member.mapper.power.MememberLegalRuleMapper;
import com.jic.member.service.power.MegradeRuleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 会员等级权益关联表
 * @author : 
 * @email : 
 * @since : 2020-04-04 10:45:04
 * @version : v1.0.0
 */
@Service
public class MegradeRuleServiceImpl implements MegradeRuleService {

    @Resource
    private MegradeRuleMapper megradeRuleMapper;
    @Resource
    private SysDeptApi sysDeptApi;
    @Resource
    MememberLegalRuleMapper memberLegalRuleMap;


    @Override
    public long insert(MegradeRule megradeRule) {
        long flag = 0;
        AdminOperate adminOperate = megradeRule.getAdminOperate();
        megradeRule.setCreatorName(adminOperate.getOperateName());
        megradeRule.setDeleteFlag(0);
        megradeRule.setGradeId(megradeRule.getId());
        megradeRule.setCreator(adminOperate.getOperator());
        megradeRule.setCreateTime(new Date());
        //先删除，后保存
        megradeRuleMapper.deleteByDeptId(megradeRule.getDeptId(),megradeRule.getId());
        megradeRule.getRuleIds().forEach(items->{
            megradeRule.setLegalRuleId(items);
            megradeRuleMapper.insert(megradeRule);
        });
        return flag;
    }

    @Override
    public long delete(Long id) {
        return megradeRuleMapper.delete(id);
    }

    @Override
    public long update(MegradeRule megradeRule) {
        return megradeRuleMapper.update(megradeRule);
    }

    @Override
    public MegradeRule queryByPrimaryKey(Long id){
        return megradeRuleMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MegradeRule> query(MegradeRule megradeRule){
        List<MegradeRule> memList = megradeRuleMapper.query(megradeRule);
        memList.forEach(items->{
            MememberLegalRule mevip=memberLegalRuleMap.queryByPrimaryKey(items.getLegalRuleId());
            items.setLegalRuleName(mevip.getLegalRuleName());
        });
        return memList;
    }

    @Override
    public PageResult<MegradeRule> queryPage(MegradeRule megradeRule, Page pageQuery) {
        //计算下标
        AdminOperate admin = megradeRule.getAdminOperate();
        megradeRule.setDeleteFlag(0);
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        megradeRule.setDeptId(admin.getDeptId());
        List<MegradeRule> list = megradeRuleMapper.queryPage(startIndex, pageQuery.getLimit(), megradeRule);
        long count = megradeRuleMapper.queryPageCount(megradeRule);
        list.forEach(items->{
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            items.setDeptName(name.getData());
        });

        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<MegradeRule> queryPageForLook(MegradeRule megradeRule, Page pageQuery) {
        //计算下标
        AdminOperate admin = megradeRule.getAdminOperate();
        megradeRule.setDeleteFlag(0);
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        if (null != megradeRule.getMerchantOrStoreName() && megradeRule.getMerchantOrStoreName() != ""){
            List<Long> sysDeptList = sysDeptApi.querymMerchantOrStoreListByName(megradeRule.getMerchantOrStoreName());
            if (CollectionUtils.isNotEmpty(sysDeptList)){
                megradeRule.setMerchantOrStoreIds(sysDeptList);
            }else {
                sysDeptList.add(0L);
                megradeRule.setMerchantOrStoreIds(sysDeptList);
            }
        }
        long count=0;
        if (null != megradeRule.getIsMom()){
            if(megradeRule.getIsMom() == 2 && admin.getDeptType().getCode() == 0){
                if (null !=megradeRule.getChildId()){
                    List<Long> childIds = new ArrayList<Long> ();
                    SysDeptRequest sysDept = new SysDeptRequest();
                    sysDept.setPid(megradeRule.getChildId());

                    List<SysDeptResponse> result = sysDeptApi.query(sysDept);
                    result.forEach(itmes->{
                        childIds.add(itmes.getId());
                    });
                    megradeRule.setChildIds(childIds);
                }
                megradeRule.setDeptId(null);
                count = megradeRuleMapper.queryPageCountForLook(megradeRule);
            }else if (megradeRule.getIsMom() == 2 && admin.getDeptType().getCode() == 1){
                count = megradeRuleMapper.queryPageCountForLook(megradeRule);
                //查询出当前子平台所有的商家店铺id
//                List<Long> merchantOrStoreList = sysDeptApi.querymMerchantOrStoreListById(admin.getDeptId());
//                if(CollectionUtils.isEmpty(merchantOrStoreList)){
//                    megradeRule.setMerchantOrStoreList(merchantOrStoreList);
//                }
            }
        }
        List<MegradeRule> list = megradeRuleMapper.queryPageForLook(startIndex, pageQuery.getLimit(), megradeRule);

        list.forEach(items->{
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            ChildNameRequest request = new ChildNameRequest();
            request.setDeptId(items.getDeptId());
            request.setDeptType(items.getDeptType());
            RestResult<String> childName = sysDeptApi.queryChildNameByDeptId(request);
            items.setDeptName(childName.getData());
            items.setMerchantOrStoreName(name.getData());
        });

        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    public long deleteByDeptId(Long deptId,Long gradeId) {
        return megradeRuleMapper.deleteByDeptId(deptId,gradeId);
    }

    @Override
    public PageResult<MegradeRule> queryMegradeRuleSimple(MegradeRule megradeRule) {
        //计算下标
        AdminOperate admin = megradeRule.getAdminOperate();
        megradeRule.setDeleteFlag(0);
        megradeRule.setDeptId(admin.getDeptId());
        long count=0;
        //查看子平台设置不需要setDeptId
        if (null != megradeRule.getIsMom() && megradeRule.getIsMom() == 1){
            megradeRule.setDeptId(null);
            megradeRule.setDeptType(1L);
        }else if (null != megradeRule.getIsMom() && megradeRule.getIsMom() == 2){
            megradeRule.setDeptId(null);
        }
        List<MegradeRule> list = megradeRuleMapper.queryMegradeRuleSimple( megradeRule);
        list.forEach(items->{
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            items.setDeptName(name.getData());
        });

        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}